package com.sl.transport.mq;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.entity.node.AgencyEntity;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.entity.node.OLTEntity;
import com.sl.transport.enums.OrganTypeEnum;
import com.sl.transport.service.IService;
import com.sl.transport.utils.OrganServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 冉申强
 * @version 1.0
 * @description: TODO
 * @date 2025/6/25 11:02
 */
@Slf4j
@Component
public class TransportListener {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value= Constants.MQ.Queues.AUTH_TRANSPORT),// 定义队列
            exchange = @Exchange(value="${rabbitmq.exchange}",type="topic"),// 定义交换机
            key="#"
    ))
    public void listenAgencyMsg(String msg) {
        //{"type":"ORG",
        // "content":[{"managerId":"1","parentId":"0","name":"天津八维营业部","id":"973902113476182273","status":true}],
        // "operation":"UPDATE"}
        log.info("接收到消息：{}", msg);
        log.info("msg");
        JSONObject jsonObject = JSONUtil.parseObj(msg);
        String type = jsonObject.getStr("type");
        if (!StrUtil.equals(type, "ORG")) {
            //非机构消息
            return;
        }
        // 取出消息内容
        String operation = jsonObject.getStr("operation");
        JSONObject contentObj = (JSONObject)jsonObject.getJSONArray("content").get(0);
        String name = contentObj.getStr("name");
        Long parentId = contentObj.getLong("parentId");
        Long bid = contentObj.getLong("id");
        Boolean status = contentObj.getBool("status");
        // 定义 IService   BaseEntity = null
        IService iService;
        BaseEntity baseEntity ;
        // 根据name判断
        if (StrUtil.endWith(name, "转运中心"))
        {
            // 如果是转运中心结尾 ==>
            //  iService = 使用工厂类根据code值获取具体实现 OLTService
            //OLTServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.OLT.getCode());
            baseEntity = new OLTEntity();
            baseEntity.setParentId(0L);
        }else if (StrUtil.endWith(name, "分拣中心"))
        {
            //TLTServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.TLT.getCode());
            baseEntity = new OLTEntity();
            baseEntity.setParentId(parentId);
        }else if (StrUtil.endWith(name, "营业部"))
        {
            //AgencyServiceImpl
            iService = OrganServiceFactory.getBean(OrganTypeEnum.AGENCY.getCode());
            baseEntity = new AgencyEntity();
            baseEntity.setParentId(parentId);
        }else {
            log.error("机构名称未按照要求填写==> {}!",msg);
            return;
        }
        baseEntity.setName(name);
        baseEntity.setBid(bid);
        baseEntity.setStatus(status);
       // 根据operation 类型
        switch (operation) {
            case "ADD":
                // 如果 ADD   iservice.保存
                iService.create(baseEntity);
            break;
            case "UPDATE":
                // 如果 UPDATE  iservice.修改
                iService.update(baseEntity);
            break;
            case "DEL":
                // 如果 DEL    iservice.删除
                iService.deleteByBid(bid);
            break;
        }

    }
}
